package com.meessiom.train.crm.collection.service;

import com.meessiom.train.crm.common.exception.ResourceNotFoundException;
import com.meessiom.train.crm.collection.model.Collection;
import com.meessiom.train.crm.collection.repository.CollectionRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;

@Service
public class CollectionService {

    @Autowired
    private CollectionRepository collectionRepository;

    public List<Collection> getAllCollections() {
        return collectionRepository.findAll();
    }

    public Collection createCollection(Collection collection) {
        return collectionRepository.save(collection);
    }

    public Collection getCollectionById(long id) {
        return collectionRepository.findById(id)
                .orElseThrow(() -> new ResourceNotFoundException("Collection not exist with id: " + id));
    }

    public Collection updateCollection(long id, Collection collectionDetails) {
        Collection updateCollection = collectionRepository.findById(id)
                .orElseThrow(() -> new ResourceNotFoundException("Collection not exist with id: " + id));

        updateCollection.setOrderId(collectionDetails.getOrderId());
        updateCollection.setCollectionDate(collectionDetails.getCollectionDate());
        updateCollection.setPeriod(collectionDetails.getPeriod());
        updateCollection.setPaymentMethod(collectionDetails.getPaymentMethod());
        updateCollection.setApprovalStatus(collectionDetails.getApprovalStatus());
        updateCollection.setAmount(collectionDetails.getAmount());
        updateCollection.setOverdueStatus(collectionDetails.getOverdueStatus());

        return collectionRepository.save(updateCollection);
    }

    public void deleteCollection(long id) {
        Collection collection = collectionRepository.findById(id)
                .orElseThrow(() -> new ResourceNotFoundException("Collection not exist with id: " + id));
        collectionRepository.delete(collection);
    }

    // 高级搜索方法 - 使用Specification实现多条件查询
    public List<Collection> searchCollections(String orderId, String approvalStatus,
                                              String overdueStatus, String paymentMethod) {
        return collectionRepository.findAll((Specification<Collection>) (root, query, criteriaBuilder) -> {
            List<Predicate> predicates = new ArrayList<>();

            if (orderId != null && !orderId.isEmpty()) {
                predicates.add(criteriaBuilder.like(root.get("orderId"), "%" + orderId + "%"));
            }
            if (approvalStatus != null && !approvalStatus.isEmpty()) {
                predicates.add(criteriaBuilder.equal(root.get("approvalStatus"), approvalStatus));
            }
            if (overdueStatus != null && !overdueStatus.isEmpty()) {
                predicates.add(criteriaBuilder.equal(root.get("overdueStatus"), overdueStatus));
            }
            if (paymentMethod != null && !paymentMethod.isEmpty()) {
                predicates.add(criteriaBuilder.equal(root.get("paymentMethod"), paymentMethod));
            }

            return criteriaBuilder.and(predicates.toArray(new Predicate[0]));
        });
    }
}